﻿using System;
using System.Data;
using System.Data.OleDb;

namespace DbManager.Connection
{
    public class ConnectionManagerOleDb : IConnectionManager
    {
        private readonly string _connectionString;

        public ConnectionManagerOleDb(string connectionString)
        {
            _connectionString = connectionString;
        }

        public bool CheckConnection()
        {
            var dbConnection = GetConnection();
            if (dbConnection != null)
            {
                var connectionIsValid = dbConnection.State == ConnectionState.Open;
                return connectionIsValid;
            }
            return false;
        }

        public IDbConnection GetConnection()
        {
            var oleDbConnection = new OleDbConnection(_connectionString);

            try
            {
                oleDbConnection.Open();
                return oleDbConnection;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}